from django.db import models

# Create your models here.


from base.models import BaseModel


class CashierCompany(BaseModel):
    name = models.CharField(max_length=128, verbose_name='公司名称', blank=True, null=True)

    class Meta:
        db_table = 'cashier_company'
        verbose_name = '公司档案'
        verbose_name_plural = verbose_name


class CashierBank(BaseModel):
    name = models.CharField(max_length=128, verbose_name='银行名称', blank=True, null=True)

    class Meta:
        db_table = 'cashier_bank'
        verbose_name = '银行档案'
        verbose_name_plural = verbose_name


class CashierStock(BaseModel):
    name = models.CharField(max_length=128, verbose_name='存货名称', blank=True, null=True)

    class Meta:
        db_table = 'cashier_stock'
        verbose_name = '存货档案'
        verbose_name_plural = verbose_name


class CashierBusiness(BaseModel):
    name = models.CharField(max_length=128, verbose_name='名称', blank=True, null=True)
    bank = models.ForeignKey(CashierBank, on_delete=models.CASCADE, verbose_name='银行', blank=True,
                             null=True)

    class Meta:
        db_table = 'cashier_business'
        verbose_name = '业务抵押表'
        verbose_name_plural = verbose_name


class CashierContract(BaseModel):
    """
    合同名
    放贷公司
    收款日期
    收款金额
    账期
    最后还款日
    利率
    备注
    # 计算属性
    还款金额合计
    未付利息
    结算最终应付利息
    还款次数
    使用金额
    """
    name = models.CharField(max_length=128, verbose_name='合同名', blank=True, null=True)
    lending_company = models.ForeignKey(CashierCompany, on_delete=models.CASCADE, verbose_name='放贷公司', blank=True,
                                        null=True)
    receive_date = models.DateField(verbose_name='收款日期', blank=True, null=True)
    received_amount = models.DecimalField(max_digits=15, decimal_places=2, verbose_name='收款金额', blank=True, null=True)
    debt_term = models.IntegerField(verbose_name='账期（天数）', blank=True, null=True)
    final_payment_date = models.DateField(verbose_name='最后还款日', blank=True, null=True)
    repayment_date = models.DateField(verbose_name='还款日期', blank=True, null=True)
    repaymen_amount = models.DecimalField(max_digits=15, decimal_places=2, verbose_name='还款金额', blank=True, null=True)
    used_day = models.IntegerField(verbose_name='使用天数', blank=True, null=True)
    annual_rate = models.DecimalField(max_digits=15, decimal_places=2, verbose_name='年利率（%）', blank=True, null=True)
    interest = models.DecimalField(max_digits=15, decimal_places=2, verbose_name='应付利息', blank=True, null=True)
    invoice_date = models.DateField(verbose_name='开票日期', blank=True, null=True)
    used_money = models.DecimalField(max_digits=15, decimal_places=2, verbose_name='使用金额', blank=True, null=True)
    note = models.CharField(max_length=128, verbose_name='备注', blank=True, null=True)

    class Meta:
        db_table = 'cashier_contract'
        verbose_name = '合同表'
        verbose_name_plural = verbose_name


class CashierLoan(BaseModel):
    # 合同名称
    name = models.CharField(max_length=128, verbose_name='名称', blank=True, null=True)
    # 银行贷款 抵押贷款 业务贷款
    loan_source = models.CharField(max_length=128, verbose_name='业务类型', blank=True, null=True)
    company = models.ForeignKey(CashierCompany, on_delete=models.CASCADE, verbose_name='贷款公司', blank=True,
                                null=True)
    risk_level = models.CharField(max_length=20, verbose_name='风险等级', blank=True, null=True)
    credit_time = models.DateTimeField(verbose_name='授信时间', blank=True, null=True)
    invoicing_time = models.DateTimeField(verbose_name='开票时间', blank=True, null=True)
    # 关联银行
    bank = models.ForeignKey(CashierBank, on_delete=models.CASCADE, verbose_name='银行', blank=True,
                             null=True)

    amount = models.DecimalField(max_digits=15, decimal_places=2, verbose_name='贷款金额', blank=True, null=True)
    # 流贷  承兑
    loan_type = models.CharField(max_length=128, verbose_name='贷款类型', blank=True, null=True)
    interest_rate = models.DecimalField(max_digits=5, decimal_places=2, verbose_name='利率', blank=True, null=True)
    margin = models.DecimalField(max_digits=15, decimal_places=2, verbose_name='保证金', blank=True, null=True)
    margin_ratio = models.DecimalField(max_digits=15, decimal_places=2, verbose_name='保证金比例', blank=True, null=True)
    margin_type = models.CharField(max_length=128, verbose_name='保证金类型', blank=True, null=True)
    expire_date = models.DateTimeField(verbose_name='到期时间', blank=True, null=True)

    # 业务贷款额外字段
    quota = models.DecimalField(max_digits=15, decimal_places=2, verbose_name='授信额度', blank=True, null=True)
    invoiced = models.DecimalField(max_digits=15, decimal_places=2, verbose_name='已开发票额度', blank=True, null=True)
    uninvoiced = models.DecimalField(max_digits=15, decimal_places=2, verbose_name='未开发票额度', blank=True, null=True)

    # 抵押业务外键
    cashier_business = models.ForeignKey(CashierBusiness, on_delete=models.CASCADE, verbose_name='抵押业务', blank=True,
                                         null=True)
    # 抵押物外键
    cashier_stocks = models.ForeignKey(CashierStock, on_delete=models.CASCADE, verbose_name='抵押物', blank=True,
                                       null=True)

    warning_time = models.IntegerField(verbose_name='提前预警时间/天', blank=True, null=True)

    class Meta:
        db_table = 'cashier_loan'
        verbose_name = '银行贷款表'
        verbose_name_plural = verbose_name


class CashierBankLoanDetails(BaseModel):
    """
    银行放贷合同明细
    房贷日期 date
    授信公司
    授信银行
    授信方式  流贷 承兑汇票
    授信额度
    保证金比例 0-100
    贷款金额
    到期日期 date
    抵押方式 string
    备注
    """
    # 合同名称
    name = models.CharField(max_length=128, verbose_name='名称', blank=True, null=True)
    # 放贷日期
    lending_date = models.DateField(verbose_name='放贷日期', blank=True, null=True)
    credit_company = models.ForeignKey(CashierCompany, on_delete=models.CASCADE, verbose_name='授信公司', blank=True,
                                       null=True)
    credit_bank = models.ForeignKey(CashierBank, on_delete=models.CASCADE, verbose_name='授信银行', blank=True,
                                    null=True)
    # 流贷 承兑汇票
    credit_way = models.CharField(max_length=128, verbose_name='授信方式', blank=True, null=True)
    line_of_credit = models.DecimalField(max_digits=15, decimal_places=2, verbose_name='授信额度', blank=True, null=True)
    margin_ratio = models.DecimalField(max_digits=15, decimal_places=2, verbose_name='保证金比例', blank=True, null=True)
    amount = models.DecimalField(max_digits=15, decimal_places=2, verbose_name='贷款金额', blank=True, null=True)
    expire_date = models.DateField(verbose_name='到期日期', blank=True, null=True)
    mortgage_way = models.CharField(max_length=128, verbose_name='抵押方式', blank=True, null=True)

    class Meta:
        db_table = 'cashier_bank_loan_details'
        verbose_name = '银行贷款合同表'
        verbose_name_plural = verbose_name


class CashierLoanDetails(BaseModel):
    """
    银行放贷执行明细
    """
    contract = models.ForeignKey(CashierBankLoanDetails, on_delete=models.CASCADE, blank=True, null=True)
    invoice_amount = models.DecimalField(max_digits=15, decimal_places=2, verbose_name='开票、开证金额', blank=True,
                                         null=True, default=0)
    flow_loan_amount = models.DecimalField(max_digits=15, decimal_places=2, verbose_name='流贷', blank=True, null=True)
    start_date = models.DateField(verbose_name='开始日期', blank=True, null=True)
    expiration_date = models.DateField(verbose_name='到期日期', blank=True, null=True)
    deposits_days = models.IntegerField(verbose_name='存款天数', blank=True, null=True)
    invoice_fee = models.DecimalField(max_digits=15, decimal_places=2, verbose_name='开票手续费', blank=True,
                                      null=True, default=0)
    cash_deposit = models.DecimalField(max_digits=15, decimal_places=2, verbose_name='保证金', blank=True, null=True)
    cash_deposit_rate = models.DecimalField(max_digits=15, decimal_places=2, verbose_name='保证金比例', blank=True,
                                            null=True)
    deposit_rate = models.DecimalField(max_digits=15, decimal_places=3, verbose_name='存款利率', blank=True, null=True)
    deposit_interest = models.DecimalField(max_digits=15, decimal_places=2, verbose_name='存款利息', blank=True, null=True)
    discount_rate = models.DecimalField(max_digits=15, decimal_places=2, verbose_name='贴现利率', blank=True, null=True)
    discount_cost = models.DecimalField(max_digits=15, decimal_places=2, verbose_name='贴现成本', blank=True, null=True)
    loan_floating = models.DecimalField(max_digits=15, decimal_places=2, verbose_name='贷款上浮', blank=True, null=True)
    loan_rate = models.DecimalField(max_digits=15, decimal_places=2, verbose_name='贷款利率', blank=True, null=True)
    cost = models.DecimalField(max_digits=15, decimal_places=2, verbose_name='成本', blank=True, null=True)

    class Meta:
        db_table = 'cashier_loan_details'
        verbose_name = '银行贷款明细表'
        verbose_name_plural = verbose_name


class ExpiryTimeControl(BaseModel):
    warning_time = models.IntegerField(verbose_name='全局提前预警时间/天', blank=True, null=True)

    class Meta:
        db_table = 'cashier_expiry_time_control'
        verbose_name = '预警时间表'
        verbose_name_plural = verbose_name


class CashierCommercialLoanDetails(BaseModel):
    """
    商业贷款明细
    收款日期 date
    收款金额
    账期（天数）
    最后还款日
    还款日期
    还款金额
    使用天数
    年利率（%）
    应付利息
    开票日期
    使用金额
    备注
    """
    lending_company = models.ForeignKey(CashierCompany, on_delete=models.CASCADE, verbose_name='放贷公司', blank=True,
                                        null=True, db_column='lending_company_id')
    receive_date = models.DateField(verbose_name='收款日期', blank=True, null=True)
    received_amount = models.DecimalField(max_digits=15, decimal_places=2, verbose_name='收款金额', blank=True, null=True)
    debt_term = models.IntegerField(verbose_name='账期（天数）', blank=True, null=True)
    final_payment_date = models.DateField(verbose_name='最后还款日', blank=True, null=True)
    repayment_date = models.DateField(verbose_name='还款日期', blank=True, null=True)
    repaymen_amount = models.DecimalField(max_digits=15, decimal_places=2, verbose_name='还款金额', blank=True, null=True)
    used_day = models.IntegerField(verbose_name='使用天数', blank=True, null=True)
    annual_rate = models.DecimalField(max_digits=15, decimal_places=2, verbose_name='年利率（%）', blank=True, null=True)
    interest = models.DecimalField(max_digits=15, decimal_places=2, verbose_name='应付利息', blank=True, null=True)
    invoice_date = models.DateField(verbose_name='开票日期', blank=True, null=True)
    used_money = models.DecimalField(max_digits=15, decimal_places=2, verbose_name='使用金额', blank=True, null=True)
    note = models.CharField(max_length=128, verbose_name='备注', blank=True, null=True)

    # 1 = 贷款金额✖️年息除以360✖️天数
    # 2 = 贷款金额✖️年息除以365✖️天数+1
    # 3 = 贷款金额✖️年息除以365✖️天数
    interest_calculation_method = models.IntegerField(verbose_name='利息计算方式', blank=True, null=True)

    # 关联合同
    contract = models.ForeignKey(CashierContract, on_delete=models.CASCADE, verbose_name='贷款合同', blank=True,
                                 null=True)

    class Meta:
        db_table = 'cashier_commercial_loan_details'
        verbose_name = '商业贷款明细表'
        verbose_name_plural = verbose_name


class CashierCommercialRemitDetail(BaseModel):
    """"""
    contract = models.ForeignKey(CashierContract, on_delete=models.CASCADE, verbose_name='贷款合同', blank=True,
                                 null=True)
    remit_amount = models.DecimalField(max_digits=15, decimal_places=2, verbose_name='汇款金额', blank=True, null=True)
    remit_date = models.DateField(verbose_name='汇款日期', blank=True, null=True)

    class Meta:
        db_table = 'cashier_commercial_remit_details'
        verbose_name = '商业贷款汇款明细表'
        verbose_name_plural = verbose_name
